using UnityEngine;

/// <summary>
/// 阳光系统测试器 - 用于测试随机阳光生成功能
/// </summary>
public class SunSystemTester : MonoBehaviour
{
    [Header("测试控制")]
    [SerializeField] private bool m_EnableTestUI = true;
    [SerializeField] private KeyCode m_ToggleRandomSunKey = KeyCode.F1;
    [SerializeField] private KeyCode m_SpawnSunKey = KeyCode.F2;
    [SerializeField] private KeyCode m_ChangeConfigKey = KeyCode.F3;
    
    [Header("测试配置")]
    [SerializeField] private SunSpawnConfig m_TestConfig;
    [SerializeField] private bool m_ShowDebugInfo = true;
    
    private SunSystem m_SunSystem;
    private bool m_IsRandomSunEnabled = true;
    
    private void Start()
    {
        // 获取阳光系统
        m_SunSystem = ServiceManager.Instance.GetService<SunSystem>();
        
        if (m_SunSystem == null)
        {
            Logger.LogError("无法获取SunSystem服务！");
            return;
        }
        
        // 如果提供了测试配置，使用它
        if (m_TestConfig != null)
        {
            m_SunSystem.SetRandomSunConfig(m_TestConfig);
        }
        
        Logger.Log("阳光系统测试器初始化完成");
        Logger.Log($"按 {m_ToggleRandomSunKey} 切换随机阳光生成");
        Logger.Log($"按 {m_SpawnSunKey} 立即生成阳光");
        Logger.Log($"按 {m_ChangeConfigKey} 切换配置");
    }
    
    private void Update()
    {
        if (m_SunSystem == null) return;
        
        // 切换随机阳光生成
        if (Input.GetKeyDown(m_ToggleRandomSunKey))
        {
            ToggleRandomSun();
        }
        
        // 立即生成阳光
        if (Input.GetKeyDown(m_SpawnSunKey))
        {
            SpawnSunImmediately();
        }
        
        // 切换配置
        if (Input.GetKeyDown(m_ChangeConfigKey))
        {
            ChangeConfig();
        }
    }
    
    /// <summary>
    /// 切换随机阳光生成
    /// </summary>
    private void ToggleRandomSun()
    {
        m_IsRandomSunEnabled = !m_IsRandomSunEnabled;
        m_SunSystem.EnableRandomSun = m_IsRandomSunEnabled;
        
        Logger.Log($"随机阳光生成: {(m_IsRandomSunEnabled ? "开启" : "关闭")}");
    }
    
    /// <summary>
    /// 立即生成阳光
    /// </summary>
    private void SpawnSunImmediately()
    {
        m_SunSystem.SpawnRandomSunImmediately();
        Logger.Log("立即生成随机阳光");
    }
    
    /// <summary>
    /// 切换配置
    /// </summary>
    private void ChangeConfig()
    {
        if (m_TestConfig != null)
        {
            // 创建新的测试配置
            var newConfig = ScriptableObject.CreateInstance<SunSpawnConfig>();
            newConfig.MinInterval = Random.Range(2f, 8f);
            newConfig.MaxInterval = Random.Range(10f, 20f);
            newConfig.MinSunValue = Random.Range(10, 20);
            newConfig.MaxSunValue = Random.Range(25, 35);
            newConfig.SpawnAreaWidth = Random.Range(15f, 25f);
            newConfig.SpawnAreaDepth = Random.Range(8f, 15f);
            newConfig.SpawnHeight = Random.Range(10f, 20f);
            newConfig.FallDuration = Random.Range(1f, 3f);
            
            m_SunSystem.SetRandomSunConfig(newConfig);
            Logger.Log("已切换到新的随机配置");
        }
    }
    
    private void OnGUI()
    {
        if (!m_EnableTestUI || m_SunSystem == null) return;
        
        GUILayout.BeginArea(new Rect(10, 10, 300, 200));
        GUILayout.BeginVertical("box");
        
        GUILayout.Label("阳光系统测试器", GUI.skin.box);
        GUILayout.Space(10);
        
        // 显示当前状态
        GUILayout.Label($"随机阳光生成: {(m_SunSystem.EnableRandomSun ? "开启" : "关闭")}");
        GUILayout.Label($"配置: {(m_SunSystem.RandomSunConfig != null ? m_SunSystem.RandomSunConfig.name : "默认")}");
        
        GUILayout.Space(10);
        
        // 控制按钮
        if (GUILayout.Button($"切换随机阳光 ({m_ToggleRandomSunKey})"))
        {
            ToggleRandomSun();
        }
        
        if (GUILayout.Button($"立即生成阳光 ({m_SpawnSunKey})"))
        {
            SpawnSunImmediately();
        }
        
        if (GUILayout.Button($"切换配置 ({m_ChangeConfigKey})"))
        {
            ChangeConfig();
        }
        
        GUILayout.Space(10);
        
        // 显示配置信息
        if (m_SunSystem.RandomSunConfig != null)
        {
            var config = m_SunSystem.RandomSunConfig;
            GUILayout.Label($"间隔: {config.MinInterval}-{config.MaxInterval}s");
            GUILayout.Label($"价值: {config.MinSunValue}-{config.MaxSunValue}");
            GUILayout.Label($"区域: {config.SpawnAreaWidth}x{config.SpawnAreaDepth}");
            GUILayout.Label($"高度: {config.SpawnHeight}");
        }
        
        GUILayout.EndVertical();
        GUILayout.EndArea();
    }
}